//
// ThreadPool.hpp for  in /home/herpec_j/Dropbox/Projets/raytracer-2016/Includes/DesignPattern/ThreadPool/
//
// Made by Jonathan
// Login   <ledey_l@epitech.net>
//
// Started on  Fri Sep 20 14:04:52 2013 Jonathan
// Last update Fri Sep 20 23:54:14 2013 Jonathan
//

#ifndef				THREADPOOL_HPP_
# define			THREADPOOL_HPP_

# include			<vector>
# include			<deque>
# include			<pthread.h>

# include			"DesignPattern/ThreadPool/Mutex.hpp"
# include			"DesignPattern/ThreadPool/CondVar.hpp"
# include			"DesignPattern/ThreadPool/Task.hpp"
# include			"DesignPattern/ThreadPool/ThreadInfo.hpp"

namespace			DesignPattern
{
  namespace			ThreadPool
  {
    class			ThreadPool
    {
    private:
      ThreadPool(ThreadPool const &rCopy);
      ThreadPool		&operator=(ThreadPool const &rCopy);

      void			InitializeThreadpool(void);
      static void		*StartThread(void *rArg);

      Mutex			mTaskMutex;
      CondVar			mTaskCondVar;
      std::vector<pthread_t>	mThreads;
      std::vector<ThreadInfo *>	mThreadInfos;
      std::deque<Task *>	mTasks;
      unsigned short		mPoolSize;
      volatile bool		mIsPoolStarted;

    protected:

    public:
      ThreadPool(void);
      ThreadPool(unsigned short rPoolSize);
      ~ThreadPool(void);

      void			AddTask(Task *rTask);
      void			*ExecuteThread(void);
      void			DestroyThreadpool(void);

      inline unsigned short	GetPoolSize(void) const
      {
	return (mPoolSize);
      }

      inline ThreadInfo		*operator[](unsigned int rIndex) const
      {
	return (mThreadInfos[rIndex]);
      }
    };
  };
};

#endif				/* THREADPOOL_HPP_ */
